package com.neu.his.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neu.his.entity.Department;
import com.neu.his.entity.DepartmentType;
import com.neu.his.entity.VO.SystemManage01.DepartmentManage.DepartmentVO;
import com.neu.his.entity.request.Department.DepartmentChange;
import com.neu.his.entity.request.Department.DepartmentParams;
import com.neu.his.mapper.DepartmentMapper;
import com.neu.his.mapper.DeptTypeMapper;
import com.neu.his.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Description: neu-his
 * <p>
 * Created by 31316 on 2020/7/15 12:32
 */
@Service
@Slf4j
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    DepartmentMapper departmentMapper;

    @Autowired
    DeptTypeMapper deptTypeMapper;

    @Override
    public boolean insertOne(DepartmentParams departmentParams) {
        //先查询是否已经有了部门
        QueryWrapper<Department> queryWrapperD = new QueryWrapper<>();
        queryWrapperD.eq("dept_name", departmentParams.getDeptName()).ne("remove_mark", 1);
        Department department = departmentMapper.selectOne(queryWrapperD);
        if (department != null) return false;
        //查询是否有部门类型
        Long id;
        DepartmentType departmentType;
        if ((departmentType = deptTypeMapper.selectOne(new QueryWrapper<DepartmentType>().ne("remove_mark", 1)
                .eq("dept_category", departmentParams.getDeptCategory())
                )) == null) return false;
        //插入
        id = departmentType.getId();
        Department department1 = new Department(departmentParams.getDeptCode(), departmentParams.getDeptName(), id);
        department1.setCreateDate();
        departmentMapper.insert(department1);
        return true;
    }

    @Override
    public boolean changeOneById(DepartmentChange departmentChange) {
        //查询是否有修改的部门类型
        Long id=-1L ;
        if (StringUtils.isNotEmpty(departmentChange.getDeptCategory())) {
            if ((id = findTypeId(departmentChange.getDeptCategory(), departmentChange.getDeptType())) == -1L)
                return false;
        }
        UpdateWrapper<Department> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set(id!=-1L,"dept_category",id);
        updateWrapper.set(StringUtils.isNotEmpty(departmentChange.getDeptCode()),"dept_code",departmentChange.getDeptCode());
        updateWrapper.set(StringUtils.isNotEmpty(departmentChange.getDeptName()),"dept_name",departmentChange.getDeptName());
        updateWrapper.eq("id",departmentChange.getId());
        updateWrapper.set("update_date",new Date());
        log.info(JSON.toJSONString(departmentChange));
        departmentMapper.update(null,updateWrapper);
        return true;
    }

    public Long findTypeId(String deptCategory, String deptType) {
        QueryWrapper<DepartmentType> queryWrapper = new QueryWrapper();
        queryWrapper.eq("dept_category", deptCategory);
        DepartmentType departmentType = deptTypeMapper.selectOne(queryWrapper);
        log.info(JSON.toJSONString(departmentType));
        if (departmentType == null) return -1L;
        return departmentType.getId();
    }
//    @Override
//    public List<DepartmentVO> getDepartment() {
//        QueryWrapper queryWrapper = new QueryWrapper();
//        List<Department> departments = departmentMapper.getDepartmentType(-1L);
//        List<DepartmentVO> departmentVOS = new ArrayList<>();
//        for (Department department : departments) {
//            DepartmentVO departmentVO = new DepartmentVO();
//            departmentVO.copy(department);
//            departmentVOS.add(departmentVO);
//        }
//        return departmentVOS;
//    }
//
//    @Override
//    public List<DepartmentVO> getDepartment(Department department) {
//        List<DepartmentVO> departmentVOS = new ArrayList<>();
//        DepartmentVO departmentVO;
//        DepartmentType departmentType1 = department.getDepartmentType();
//        QueryWrapper queryWrapper = new QueryWrapper();
//        if (StringUtils.isNotEmpty(department.getDeptName()))
//            queryWrapper.eq("dept_name", department.getDeptName());
//        if (StringUtils.isNotEmpty(department.getDeptCode())) ;
//        queryWrapper.eq("dept_code", department.getDeptCode());
//        List<Department> departments = departmentMapper.selectList(queryWrapper);
//        for (Department departmentSelect : departments) {
//            List<DepartmentType> departmentTypes = (List<DepartmentType>) deptTypeMapper.selectById(departmentSelect.getDeptCategory());
//            DepartmentType departmentType = departmentTypes.get(0);
//
//            if(departmentType1!=null) {
//                if (StringUtils.isNotEmpty(departmentType1.getDeptCategory()) && departmentType1.getDeptCategory() != departmentType.getDeptCategory()
//                        || StringUtils.isNotEmpty(departmentType1.getDeptType()) && departmentType1.getDeptType() != departmentType.getDeptType()) {
//                    continue;
//                }
//                departmentSelect.setDepartmentType(departmentType);
//            }
//            departmentSelect.setDepartmentType(departmentType);
//            departmentVO = new DepartmentVO();
//            departmentVO.copy(departmentSelect);
//            departmentVOS.add(departmentVO);
//        }
//        return departmentVOS;
//    }


}
